﻿Public Class StockVaccins_Screen

    Private _ctrl As StockVaccins_CTRL
    Private _flagMaJ

    Public Sub New(ByVal ctrl As StockVaccins_CTRL)

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        _ctrl = ctrl
        RafraichirListe()
        _flagMaJ = False
    End Sub

    Private Sub RafraichirListe()
        DGVvaccins.DataSource = Nothing
        DGVvaccins.DataSource = _ctrl.getListeVaccins
        DGVMiseAjourStock.DataSource = Nothing
        DGVMiseAjourStock.DataSource = _ctrl.getListeVaccins

    End Sub


    Private Sub DGVvaccins_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVvaccins.CellFormatting
        For Each row As DataGridViewRow In DGVvaccins.Rows
            Select Case row.Cells(1).Value
                Case Is < 5
                    row.Cells(1).Style.BackColor = Color.Red
                Case Else
                    row.Cells(1).Style.BackColor = row.Cells(0).Style.BackColor
            End Select
        Next
    End Sub

    Private Sub BTNlivraison_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNlivraison.Click
        If _flagMaJ Then
            If isSaisieOK() Then
                _ctrl.maJStockVaccins()
                DGVvaccins.Visible = True
                DGVMiseAjourStock.Visible = False
                BTNlivraison.Text = "Mise à jour du stock"
                _flagMaJ = False
            Else
                MessageBox.Show("Erreur de mise à jour des stocks de vaccins : vérifiez les données saisies", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End If
        Else
            DGVvaccins.Visible = False
            DGVMiseAjourStock.Visible = True
            BtnRetour.Visible = True
            RafraichirListe()
            BTNlivraison.Text = "Valider"
            _flagMaJ = True
            Debug.WriteLine(DGVMiseAjourStock.Columns(2).CellTemplate.ToString & _
                            vbCrLf & _
                            DGVMiseAjourStock.Columns(2).CellType.ToString)
            CType(DGVMiseAjourStock.Columns(2), DataGridViewComboBoxColumn).DataSource = LectureFichierFournisseur.getListeFournisseur
        End If
    End Sub

    Private Function isSaisieOK() As Boolean
        isSaisieOK = False
        For Each row As DataGridViewRow In DGVMiseAjourStock.Rows
            If row.Cells(1).Value IsNot Nothing And row.Cells(2).Value IsNot Nothing Then
                Dim result As Integer
                If Integer.TryParse(row.Cells(1).Value.ToString, result) _
                    AndAlso result > CType(row.DataBoundItem, Vaccins).QuantiteStock Then
                    _ctrl.setNouveauStock(row.DataBoundItem, result)
                    isSaisieOK = True
                End If
            End If

        Next

    End Function


    Private Sub BtnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRetour.Click
        DGVvaccins.Visible = True
        DGVMiseAjourStock.Visible = False
        BTNlivraison.Text = "Mise à jour du stock"
        _flagMaJ = False
        BtnRetour.Visible = False
    End Sub
End Class